import superModule from "./superModule";
import {GHpLocalPropertyInfo, GHpNormData} from "./httpContent/gameHttpContent";

export default class GameHttpModule extends superModule {
    protected m_className: string = '网络http模块'; 
    //URL
    private readonly GAME_SERVER_URL: string = 'http://api.xiayiqu.com';

    PingPong(cb: (res: any) => void, errorCb?: (res: GHpNormData) => void) {
        // this.sendHttp(web_api.POST.ping_pong, 'post', null, cb, errorCb);
    }

    /**
     * 获取用户信息
     * @param wxIdentify 唯一标识
     * @param cb
     * @param errorCb
     */
    getUserWebInfo(wxIdentify: string, cb: (res: any) => void, errorCb?: (res: GHpNormData) => void) {
        
    }

    getLocalPropertyInfo(cb: (res: GHpLocalPropertyInfo) => void, errorCb?: (res: GHpNormData) => void) {
        let resData = <GHpLocalPropertyInfo>{};
        this.sendHttp('/dataCenter/api/ip/getClientInfo', 'get', null, resData, cb, res => {
            console.error('#38 http错误', res);
            errorCb && errorCb(res);
        });
    }

    /***
     * 发送http条文
     * @param router 无需URL 路径
     * @param method 提交方式 GET POST
     * @param params 参数对象
     * @param resData 返回数据
     * @param successCallback 提交成功
     * @param errorCallback 提交失败
     * @param async 是否异步 - 默认 true
     */
    private sendHttp<T>(router: string,
                        method: string,
                        params: object,
                        resData: T,
                        successCallback: (res: T) => void,
                        errorCallback?: (res: GHpNormData) => void,
                        async = true) {
        let url = this.GAME_SERVER_URL + router;
        let data = null;
        if (params) {
            //
            for (let k in params) {
                let v = params[k];
                if (data == null) {
                    data = `${k}=${v}`;
                } else {
                    data += `&${k}=${v}`;
                }
            }
            //
        }
        let xhr = new XMLHttpRequest();
        xhr.timeout = 10000;
        xhr.onreadystatechange = () => {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    let response = xhr.responseText;
                    try {
                        this.onHttpDone(JSON.parse(response), (res) => {
                            switch (typeof res) {
                                case "object": {
                                    successCallback(<T>res);
                                    break;
                                }
                                case "string": {
                                    successCallback(<T>JSON.parse(res));
                                    break;
                                }
                            }
                        }, errorCallback, xhr);
                    } catch (e) {
                        console.error('运行后端回执引发的异常', response, e);
                    }
                } else {
                    errorCallback && errorCallback(null);
                }
            }
        };
        xhr.ontimeout = () => {
            console.error('请求超时', url);
            errorCallback && errorCallback({code: -1, msg: '请求超时', data: null});
        }
        if (method.toUpperCase() == 'GET' && data) {
            url += `?${data}`;
        }
        xhr.open(method, url, async);
        xhr.setRequestHeader('Content-type', 'application/json');
        if (method.toUpperCase() == 'POST') {
            xhr.send(JSON.stringify(params));
        } else {
            xhr.send();
        }
    }

    /**
     * @param {HttpData} jsonResponse
     * */
    private onHttpDone(jsonResponse: GHpNormData, successCallback: (res: any | string) => void, errorCallback: (res: GHpNormData) => void, xhr: XMLHttpRequest) {
        let {code, msg, data} = jsonResponse;

        if (code != null && code != 0) {
            errorCallback && errorCallback(jsonResponse);
            console.error('错误的回执', jsonResponse);
            return;
        }
        //
        successCallback(data || jsonResponse);
    }
}//电子邮件puhalskijsemen@gmail.com
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999
